import { clearLoginInfo } from '@/utils'
import { CONFIG } from './index.js'

export const uploadFile = (options) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${CONFIG.baseUrl}/${options.url}`,
			header: options.headers || { 
				'Authorization': 'Bearer ' + uni.getStorageSync('token'),
				// 'Content-Type': 'multipart/form-data'
			},
			name: 'file',
			files: options.files ? options.files : null,
			filePath: options.filePath,
			formData: options.formData || {},
			timeout: options.timeout || 20 * 1000,
			success(res) {
				if (res.data.code == 401) {
					clearLoginInfo()
					return
				} else {
					resolve(res.data)
				}
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

export default uploadFile